package common

import (
	"ai-bot/entity"
	bytes2 "bytes"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/opentdp/wrest-chat/wcferry"
	"io/ioutil"
	"net/http"
)

func Post(bytes []byte, url string, method string) []byte {
	client := &http.Client{}
	req, err := http.NewRequest(method, url, bytes2.NewBuffer(bytes))

	if err != nil {
		fmt.Println(err)
		return nil
	}
	req.Header.Add("accept", "application/json")
	req.Header.Add("Content-Type", "application/json")

	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return nil
	}
	defer res.Body.Close()
	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return nil
	}
	return body
}

func IsContain[T int | int8 | int32 | int64 | string](items []T, item T) bool {
	for _, eachItem := range items {
		if eachItem == item {
			return true
		}
	}
	return false
}

func Receive(ctx *gin.Context) {
	msg := wcferry.FlexWxMsg{}
	ctx.BindJSON(&msg)
	fmt.Println(msg)
	fmt.Println("msg.Content: ", msg.Content)
	fmt.Println("msg.Id: ", msg.Id)
	fmt.Println("msg.Type: ", msg.Type)
	fmt.Println("msg.Extra: ", msg.Extra)
	fmt.Println("msg.IsGroup: ", msg.IsGroup)
	fmt.Println("msg.IsSelf: ", msg.IsSelf)
	fmt.Println("msg.Roomid: ", msg.Roomid)
	fmt.Println("msg.Sender: ", msg.Sender)
	fmt.Println("msg.Sign: ", msg.Sign)
	fmt.Println("msg.Thumb: ", msg.Thumb)
	fmt.Println("msg.Ts: ", msg.Ts)
	fmt.Println("msg.GetContent(): ", msg.GetContent())
	fmt.Println("msg.Xml: ", msg.Xml)
	// 艾特一个人
	//  map[msgsource:map[atuserlist:wxid_fitxnaxdc83s22 membercount:4 pua:1 signature:V1_kmLTFoXU|v1_kmLTFoXU silence:0 tmp_node:map[publisher-id:]]]
	// 艾特多个人
	// map[msgsource:map[atuserlist:wxid_2qhmyothjjdr22,wxid_fitxnaxdc83s22 membercount:4 pua:1 signature:V1_w98PnpH5|v1_w98PnpH5 silence:0 tmp_node:map[publisher-id:]]]
	// 没有艾特人
	// map[msgsource:map[membercount:4 pua:1 signature:V1_lJsX16iy|v1_lJsX16iy silence:0 tmp_node:map[publisher-id:]]]

	//var xml interface{} = msg.Xml
	//at, b := wechat.IsAt(xml)
	//if !b {
	//	return
	//}
	//split := strings.Split(at, ",")
	//userInfo := entity.Blue7Bot1.SelfInfo
	//if userInfo != nil {
	//	payload := userInfo.Payload
	//	wxid := payload.Wxid
	//	if IsContain(split, wxid) && IsContain(entity.Blue7Bot1.ChatRoomIds, msg.Roomid) {
	//		entity.Blue7Bot1.FlexWxMsgChan <- msg
	//	}
	//}
	entity.Blue7Bot1.FlexWxMsgChan <- msg
	fmt.Println("------------->")

}

// CallAi 调用 ai模型 获取数据
type CallAi func(string) string
